package com.imut.lagain.repository;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.FinalPassword;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 最终密码数据访问层
 */
@Mapper
public interface FinalPasswordRepository extends BaseMapper<FinalPassword> {
    
    /**
     * 查询当前激活的密码
     * @return 激活的密码
     */
    @Select("SELECT * FROM final_password WHERE is_active = 1 AND is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findActivePassword();
    
    /**
     * 查询所有激活的密码列表
     * @return 激活的密码列表
     */
    @Select("SELECT * FROM final_password WHERE is_active = 1 AND is_deleted = 0 ORDER BY create_time DESC")
    List<FinalPassword> findAllActivePasswords();
    
    /**
     * 根据密码值查询（用于验证）
     * @param password 密码值
     * @return 密码信息
     */
    @Select("SELECT * FROM final_password WHERE password = #{password} AND is_deleted = 0")
    FinalPassword findByPassword(@Param("password") String password);
    
    /**
     * 根据激活状态查询密码列表
     * @param isActive 是否激活
     * @param page 分页参数
     * @return 密码分页列表
     */
    @Select("SELECT * FROM final_password WHERE is_active = #{isActive} AND is_deleted = 0 ORDER BY create_time DESC")
    IPage<FinalPassword> findByActiveStatus(@Param("isActive") Boolean isActive, Page<FinalPassword> page);
    
    /**
     * 查询指定时间范围内创建的密码
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 密码列表
     */
    @Select("SELECT * FROM final_password WHERE create_time BETWEEN #{startTime} AND #{endTime} AND is_deleted = 0 ORDER BY create_time DESC")
    List<FinalPassword> findByCreateTimeBetween(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
    
    /**
     * 查询指定时间范围内创建的密码（分页）
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 分页参数
     * @return 密码分页列表
     */
    @Select("SELECT * FROM final_password WHERE create_time BETWEEN #{startTime} AND #{endTime} AND is_deleted = 0 ORDER BY create_time DESC")
    IPage<FinalPassword> findByCreateTimeBetweenWithPage(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, Page<FinalPassword> page);
    
    /**
     * 统计激活的密码数量
     * @return 激活密码数量
     */
    @Select("SELECT COUNT(*) FROM final_password WHERE is_active = 1 AND is_deleted = 0")
    Long countActivePasswords();
    
    /**
     * 统计总密码数量
     * @return 总密码数量
     */
    @Select("SELECT COUNT(*) FROM final_password WHERE is_deleted = 0")
    Long countTotalPasswords();
    
    /**
     * 更新密码激活状态
     * @param passwordId 密码ID
     * @param isActive 是否激活
     * @return 影响行数
     */
    @Update("UPDATE final_password SET is_active = #{isActive}, update_time = NOW() WHERE id = #{passwordId}")
    int updateActiveStatus(@Param("passwordId") Long passwordId, @Param("isActive") Boolean isActive);
    
    /**
     * 停用所有密码
     * @return 影响行数
     */
    @Update("UPDATE final_password SET is_active = 0, update_time = NOW() WHERE is_active = 1 AND is_deleted = 0")
    int deactivateAllPasswords();
    
    /**
     * 根据描述模糊查询密码
     * @param description 描述关键字
     * @param page 分页参数
     * @return 密码分页列表
     */
    @Select("SELECT * FROM final_password WHERE description LIKE CONCAT('%', #{description}, '%') AND is_deleted = 0 ORDER BY create_time DESC")
    IPage<FinalPassword> findByDescriptionLike(@Param("description") String description, Page<FinalPassword> page);
    
    /**
     * 检查密码是否存在
     * @param password 密码值
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) > 0 FROM final_password WHERE password = #{password} AND is_deleted = 0")
    boolean existsByPassword(@Param("password") String password);
    
    /**
     * 检查激活的密码是否存在
     * @param password 密码值
     * @return 是否存在且激活
     */
    @Select("SELECT COUNT(*) > 0 FROM final_password WHERE password = #{password} AND is_active = 1 AND is_deleted = 0")
    boolean existsByPasswordAndActive(@Param("password") String password);
    
    /**
     * 获取最新创建的密码
     * @return 最新密码
     */
    @Select("SELECT * FROM final_password WHERE is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findLatestPassword();
    
    /**
     * 获取最新更新的密码
     * @return 最新更新的密码
     */
    @Select("SELECT * FROM final_password WHERE is_deleted = 0 ORDER BY update_time DESC LIMIT 1")
    FinalPassword findLatestUpdatedPassword();
    
    /**
     * 检查密码值是否存在
     * @param passwordValue 密码值
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) > 0 FROM final_password WHERE password = #{passwordValue} AND is_deleted = 0")
    boolean existsByPasswordValue(@Param("passwordValue") String passwordValue);
    
    /**
     * 查询最新创建的密码
     * @return 最新创建的密码
     */
    @Select("SELECT * FROM final_password WHERE is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findLatestCreatedPassword();
    
    /**
     * 查询当前激活的密码
     * @return 当前激活的密码
     */
    @Select("SELECT * FROM final_password WHERE is_active = 1 AND is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findCurrentActivePassword();
    
    /**
     * 根据密码值查询密码
     * @param passwordValue 密码值
     * @return 密码对象
     */
    @Select("SELECT * FROM final_password WHERE password = #{passwordValue} AND is_deleted = 0")
    FinalPassword findByPasswordValue(@Param("passwordValue") String passwordValue);
}
